package kr.hkit.powerjava.chapter26.h1;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;

public class DBUtil {
	public static Properties loadProperties(String propertiesFile){
		BufferedInputStream bis = null;
		Properties prop = null;
		
		try {
			bis = new BufferedInputStream(new FileInputStream(propertiesFile));
			prop = new Properties();
			prop.loadFromXML(bis);
		} catch (FileNotFoundException e) {
			System.err.println("파일이 존재하지 않음");
		} catch (InvalidPropertiesFormatException e) {
			System.err.println("유효하지 않음");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(bis != null)
				try{
					bis.close();
					bis = null;
				}catch(Exception e){
					
				}
				}
		return prop;
	}
	
	public static void saveProperties(String propertiesFiles, String...value){
		BufferedOutputStream bos = null;
		Properties prop = new Properties();
		
		try {
			bos = new BufferedOutputStream(new FileOutputStream(propertiesFiles));
			prop.setProperty("DRIVER", value[0]);
			prop.setProperty("URL", value[1]);
			prop.setProperty("USER", value[2]);
			prop.storeToXML(bos, "DabaBase 커넥션 xml 속성 파일임!!!");
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(bos != null)
				try{
					bos.close();
					bos = null;
				}catch (Exception e){
					
				}
		}
		
	}
}
